var initialOptions = [];
var _ = require("lodash");
function carts(state = initialOptions, action) {
  if(action.payload){

    action.payload = JSON.parse(JSON.stringify(action.payload))
  }
  //2.创建数据中心
  switch (action.type) {
    case "ADDTOCART": 
      var index = _.findIndex(state, action.payload);
      if(index==-1){
          action.payload.num = 1
            return [...state,action.payload]
      }else{
        state[index].num =state[index].num>=99?99: state[index].num+1
            return [...state]
      }
      case "REDUCETOCART": 
      var index = _.findIndex(state, action.payload);
      if(index==-1){

          action.payload.num = 1
            return [...state,action.payload]
      }else{
        
        state[index].num = state[index].num-1
        if(state[index].num==0){
          delete state[index]
        }
        var arr=state.filter((value, index) => {
          return value
        })
     
            return [...arr]
      }
    default: 
      return state;
  }
}

export default carts;
